package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 听雨楼
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired(required = false)
    private SetmealMapper setmealMapper;
    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;
    @Autowired(required = false)
    private CategoryMapper categoryMapper;
    @Autowired
    private HttpSession session;
//    @CacheEvict(value = "setmealCache",allEntries = true)
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //补全setmeal的信息
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealDto.setStatus(1);
        //调用保存的方法
        setmealMapper.saveWithDish(setmealDto);
//得到套餐的菜品，给所有的菜品补全信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<Object> collect = setmealDishes.stream().map(setmealDish -> {
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setSort(0);
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;
        }).collect(Collectors.toList());
        //批量插入
        setmealDishMapper.saveWithDish(collect);

    }


    /**
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> page(int page, int pageSize, String name) {
        //设置当前页面与页面的大小
        PageHelper.startPage(page, pageSize);
        //根据name查询数据，得到list集合 list
        List<Setmeal> setmealList = setmealMapper.page(page, pageSize, name);
        //创建pageinfo对象，只有创建pageifo对象了，上面的sql语句才会真正的去执行
        PageInfo<Setmeal> setmealPageInfo = new PageInfo<>(setmealList);
        //遍历list《setmeal》，把setmeal转换为setmealdto
        List<SetmealDto> collect = setmealPageInfo.getList().stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Category category = categoryMapper.findById(setmealDto.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        //换成page
        Page<SetmealDto> setmealDtoPage = new Page<>(collect, setmealPageInfo.getTotal(), page, pageSize);
        return setmealDtoPage;


    }

    /**
     *
     * @param ids
     */
//    @CacheEvict(value = "setmealCache",allEntries = true)
    @Override//批量删除
    public void delete(@RequestParam List<Long> ids) {
        Long count = setmealMapper.queryWithSetmealDishStatus(ids);
        if (count > 0) {
            throw new CustomException("改套餐不能删除！");
        }
        setmealMapper.deleteByIdsWithDish(ids);
        setmealDishMapper.deleteByIdsWithDish(ids);
    }

    /**
     *
     * @param id
     * @return
     */
    //通过id来查询
    @Override
    public SetmealDto findById(Long id) {
        Setmeal setmeal = setmealMapper.findByIdAndDish(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        List<SetmealDish> setmealDishList = setmealDishMapper.findByIdAndDish(setmealDto.getId());
        setmealDto.setSetmealDishes(setmealDishList);
        return setmealDto;
    }

    /**
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    @CacheEvict(value = "setmealCache",allEntries = true)
    public void update(SetmealDto setmealDto) {
        //补全setmeal的信息
        setmealDto.setUpdateTime(LocalDateTime.now());
        //调用修改的方法
        setmealMapper.updateWithDish(setmealDto);
        setmealDishMapper.deleteByIdsWithDishon(setmealDto.getId());
//得到套餐的菜品，给所有的菜品补全信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<Object> collect = setmealDishes.stream().map(setmealDish -> {
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setSort(0);
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;
        }).collect(Collectors.toList());
        //批量插入
        setmealDishMapper.saveWithDish(collect);
    }

    /**
     *
     * @param ids
     */
    @Override//批量停售
    public void updateStatusZero(List<Long> ids) {
        Long empId = (Long) session.getAttribute("employee");
        LocalDateTime updateTime = LocalDateTime.now();
        setmealMapper.updateStatusZero(ids, empId, updateTime);
    }

    /**
     *
     * @param ids
     */
    @Override
    public void updateStatusOne(List<Long> ids) {
        Long empId = (Long) session.getAttribute("employee");
        LocalDateTime updateTime = LocalDateTime.now();
        setmealMapper.updateStatusOne(ids, empId, updateTime);
    }

    /**
     *
     * @param categoryId
     * @param status
     * @return
     */
    @Cacheable(value = "setmealCache",key = "#categoryId+'_'+#status")
    @Override
    public List<SetmealDto> list(Long categoryId, Integer status) {
        List<Setmeal> setmealList = setmealMapper.list(categoryId, status);
        List<SetmealDto> collect = setmealList.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Category category = categoryMapper.findByCategoryId(setmeal.getCategoryId());
            List<SetmealDish> setmealDishes = setmealDishMapper.findByIdAndDish(setmeal.getId());
            setmealDto.setSetmealDishes(setmealDishes);
            return setmealDto;
        }).collect(Collectors.toList());

        return collect;

    }
}
